﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KhuyenMaiVang.DAL;

namespace KhuyenMaiVang.Areas.Admin.Controllers
{ 
    public class BusinessController : Controller
    {
        private KhuyenMaiVangDataContext db = new KhuyenMaiVangDataContext();

        //
        // GET: /Admin/Business/

        public ViewResult Index()
        {
            ViewBag.TempDataSuccess = (String)TempData["success"];
            var businesses = db.Businesses.Include("User");
            return View(businesses.ToList());
        }

        //
        // GET: /Admin/Business/Details/5

        public ViewResult Details(int id)
        {
            Business business = db.Businesses.Single(b => b.ID == id);
            return View(business);
        }

        //
        // GET: /Admin/Business/Create

        public ActionResult Create()
        {
            List<User> lstUsers = db.Users.Where(m => m.Status == 1 && m.Group.Name == "Business").ToList();
            ViewBag.UserID = new SelectList(lstUsers, "ID", "Username");
            return View();
        } 

        //
        // POST: /Admin/Business/Create

        [HttpPost]
        public ActionResult Create(Business business)
        {
            if (ModelState.IsValid)
            {
                db.Businesses.AddObject(business);
                db.SaveChanges();
                TempData["success"] = "Đã thêm doanh nghiệp:" + business.MangerName;
                return RedirectToAction("Index");  
            }

            ViewBag.UserID = new SelectList(db.Users, "ID", "Username", business.UserID);
            return View(business);
        }
        
        //
        // GET: /Admin/Business/Edit/5
 
        public ActionResult Edit(int id)
        {
            Business business = db.Businesses.Single(b => b.ID == id);
            ViewBag.UserID = new SelectList(db.Users, "ID", "Username", business.UserID);
            return View(business);
        }

        //
        // POST: /Admin/Business/Edit/5

        [HttpPost]
        public ActionResult Edit(Business business)
        {
            if (ModelState.IsValid)
            {
                db.Businesses.Attach(business);
                db.ObjectStateManager.ChangeObjectState(business, EntityState.Modified);
                db.SaveChanges();
                TempData["success"] = "Đã sửa doanh nghiệp:" + business.MangerName;
                return RedirectToAction("Index");
            }
            ViewBag.UserID = new SelectList(db.Users, "ID", "Username", business.UserID);
            return View(business);
        }

        //
        // GET: /Admin/Business/Delete/5
 
        public ActionResult Delete(int id)
        {
            Business business = db.Businesses.Single(b => b.ID == id);
            return View(business);
        }

        //
        // POST: /Admin/Business/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Business business = db.Businesses.Single(b => b.ID == id);
            db.Businesses.DeleteObject(business);
            db.SaveChanges();
            TempData["success"] = "Đã xóa doanh nghiệp:" + business.MangerName;
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}